package com.sun.dream.fish.secret.hash;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA2Example {
    public static void main(String[] args) {
        String input = "Hello World";

        try {
            String sha256Hash = getSHA256Hash(input);
            System.out.println("SHA-256 hash of \"" + input + "\": " + sha256Hash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public static String getSHA256Hash(String input) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] messageDigest = md.digest(input.getBytes(StandardCharsets.UTF_8));

        StringBuilder hashText = new StringBuilder();

        for (byte b : messageDigest) {
            hashText.append(String.format("%02x", b));
        }

        return hashText.toString();
    }
}